
$(function () {
	var re = /^[0-9]+\.?[0-9]*$/;   //判断字符串是否为数字
	var activityId_hidden = $("#activityId").val();
	var prizeType = 0;
	
	//计算中奖概率,中奖比例=投放数量/参奖基数
	$("#nub").blur(function(){
		var nub = parseInt($("#actBaseNub").val());//参奖基数
		var nubNum = $("#nub").val();
		$("#odds").val("").siblings(".oddsNum").html(""); 
		 if(nubNum){
			 if (!re.test(nubNum)){
				 bootbox.balert("投放数量必须为数字!");
			 } else {
				 var n = parseInt(delcommafy($("#nub").val()));
				 
				 if(n/nub*100 > 100){
					 bootbox.balert("投放数量必须小于等于参奖基数,请修改投放数量!");
				 } else {
					 $("#odds").val((n/nub*100).toFixed(2)).siblings(".oddsNum").html( (n/nub*100).toFixed(2) + "%"); 
				 }
			 }
			
		 }
	});
	
	//自定义奖品等级
	$("#chk_custom").click(function () {
		if($("#chk_custom").is(":checked")){
			$("#gradeName").attr("disabled", "disabled");//奖品等级下拉框置灰
			$("#grandName_custom").attr("disabled", false);//放开自定义奖品名称，需要输入名称
		}else{
			$("#gradeName").attr("disabled", false);//奖品等级下拉框置灰
			$("#grandName_custom").attr("disabled", true);//放开自定义奖品名称，需要输入名称
		}
	});
	
	//取消中奖规则
	$("[name='btn_cancel_new_activity_prize']").click(function(){
		var activityPrizeId = $(this).data("activityprize-id");//中奖规则id
		window.location.href="/activity/getActivityPrizeList?activityId="+activityId_hidden;
	});
	//保存活动中奖规则
    $("[name='btn_save_new_activity_prize']").click(function () {
    	var activityId = activityId_hidden;
        var form = window.form_new_activity_prize;
        
        var gradeName = "";
        if($("#chk_custom").is(":checked")){
        	if($("#grandName_custom").val()==null || $("#grandName_custom").val()==""){
        		bootbox.balert("自定义等级必须输入等级名称!");
        		return false;
        	}else{
        		gradeName = $("#grandName_custom").val();
        	}
        }else{
        	if($('#gradeName option:selected').val()=="" ||$('#gradeName option:selected').val()==null){
        		bootbox.balert("请选择奖品等级!");
        		return false;
        	}else{
        		var gradeName = $('#gradeName option:selected').text();
        	}
        }
        
        var prizeId = form.prizeId.value;
        if(!prizeId){
        	bootbox.balert("奖品名称必须选择!");
    		return;
        }
        var sameWinningLimitTimes = form.sameWinningLimitTimes.value
        if (!re.test(sameWinningLimitTimes))
        {
        	bootbox.balert("中奖次数限制必须为数字!");
            return false;
        }
        var nub = form.nub.value;
        if (!re.test(nub))
        {
        	bootbox.balert("投放数量必须为数字!");
            return false;
         }
        var odds = form.odds.value;
        if (odds>100)
        {
        	bootbox.balert("中奖概率不能大于100%,请修改投放数量!");
            return false;
         }
        var maxWinnngTimes = form.maxWinnngTimes.value;
        if (!re.test(maxWinnngTimes))
        {
        	bootbox.balert("最大中奖限制数量必须为数字!");
            return false;
         }

         var isAttention=$(".isAttention:checked").val();
         if(prizeType == 1){
            if(isAttention=='' || isAttention==undefined){
                bootbox.balert("请选择是否需要关注公众号领取!");
                return false;
            }
         }
        var isInput = form.isInput.value;
        
        var activityPrize = {activityId: activityId,gradeName:gradeName,prizeId:prizeId,sameWinningLimitTimes:sameWinningLimitTimes,nub:nub,odds:odds,maxWinnngTimes:maxWinnngTimes,isAttention:isAttention,isInput:isInput};
        $.postJsonBody("/activity/addPrize", activityPrize, function (re) {
            // tipper.success("已保存，请刷新页面");
            // $("#md_new_brand").modal("hide");
        	window.location.href="/activity/getActivityPrizeList?activityId="+activityId_hidden;
        })
    });
    
    
  //删除中奖规则
    $("[name='btn_delete_activityPrize']").click(function(){
    	var activityPrizeId = $(this).data("activityprize-id");//中奖规则id
    	
    	bootbox.bconfirm("确定要删除该规则吗？", function (re) {
            if (re) {
                $.delete("/activity/deleteActivityPrize/" + activityPrizeId, function (re) {
                    if (re.data) {
                    	window.location.href="/activity/getActivityPrizeList?activityId="+activityId_hidden;
                    } else {
                        tipper.danger("删除失败，请重试");
                    }
                });
            }
        });
    });
    
    
  //加载异步数据,奖品名称
    $.get("/prize", function (re) {
        var prizeList = re.data;
        var form = window.form_new_activity_prize;
        var brandOptions = "";
        
        for (var i in prizeList) {
            var prize = prizeList[i];
            brandOptions += "<option value='" + prize.id + "'>" + prize.name + "</option>";
        }
        if(window.form_new_activity_prize != null){
        	$(window.form_new_activity_prize.prizeId).append(brandOptions);
        }
        if(window.form_edit_activity_prize != null){
        	$(window.form_edit_activity_prize.prizeId).append(brandOptions);
        }
    });
    
  //选择奖品
	$("[name='prizeId']").change(function(){
		var id = $("#prize option:selected").val();
		$.get("/prize/" + id, function (re) {
	        var prize = re.data;
	        var type="";
            prizeType = prize.type;
	        if(prizeType==1){
	        	type="红包";
	        	$("#div_attention").removeClass("hide");
//	        	$("#div_Input").removeClass("hide");
	        	
	        }else if(prizeType ==2){
	        	type="实物";
                $("#div_attention").addClass("hide");
//                $("#div_Input").addClass("hide");
	        }else{
	        	type="优惠券";
                $("#div_attention").addClass("hide");
//                $("#div_Input").addClass("hide");
	        }
	        $("#prizeType").text(type);
	        $("#prizePrice").text(toDecimal2(prize.price));
	        
	    });
	});
    
    
});
  
//制保留2位小数，如：2，会在2后面补上00.即2.00    
function toDecimal2(x) {    
    var f = parseFloat(x);    
    if (isNaN(f)) {    
        return false;    
    }    
    var f = Math.round(x*100)/100;    
    var s = f.toString();    
    var rs = s.indexOf('.');    
    if (rs < 0) {    
        rs = s.length;    
        s += '.';    
    }    
    while (s.length <= rs + 2) {    
        s += '0';    
    }    
    return s;    
}    

/**  
 * 去除千分位  
 *@param{Object}num  
 */  
function delcommafy(num){  
   num = num.replace(/[ ]/g, "");//去除空格  
   num=num.replace(/,/gi,'');  
   return num;  
}    


